import { createStore, Store, useStore as useVuexStore } from 'vuex'

import type { IRootState, IStoreType } from './types'

import login from './login/login'
import { getPageListData } from 'network/main/system/system'
import dashboard from './main/analysis/dashboard'
import system from './main/system/system'
const state = () => ({
  name: '胭脂',
  age: 18,
  entireDepartment: [],
  entireRole: [],
  entireMenu: []
})

const getters = {}

const store = createStore<IRootState>({
  state,
  getters,
  mutations: {
    changeEntireDepartment(state, list) {
      state.entireDepartment = list
    },
    changeEntireRole(state, list) {
      state.entireRole = list
    },
    changeEntireMenu(state, list) {
      state.entireMenu = list
    }
  },
  actions: {
    async getInitailDataAction({ commit }) {
      // 请求部门和角色数据
      const departmentResult = await getPageListData('/department/list', {
        offset: 0,
        size: 10000
      })
      const { list: departmentList } = departmentResult.data

      const roleResult = await getPageListData('/role/list', {
        offset: 0,
        size: 10000
      })
      const { list: roleList } = roleResult.data

      const menuReslt = await getPageListData('/menu/list', {})
      const { list: menuList } = menuReslt.data

      // 保存数据
      commit('changeEntireDepartment', departmentList)
      commit('changeEntireRole', roleList)
      commit('changeEntireMenu', menuList)
    }
  },
  modules: {
    login,
    system,
    dashboard
  }
})

export function setupStore() {
  store.dispatch('login/loadLocalLogin')
}

export function useStore(): Store<IStoreType> {
  return useVuexStore()
}

export default store
